package com.hoten.gulimall.product.web;

import com.hoten.gulimall.product.entity.CategoryEntity;
import com.hoten.gulimall.product.service.CategoryService;
import com.hoten.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping({"/", "/index.html"})
    public String indexPage(Model model) {


        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();

        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    @ResponseBody
    @RequestMapping("index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @ResponseBody
    @GetMapping("hello")
    public String hello(){
        RLock lock = redisson.getLock("my-lock");
        lock.lock();
        try{
            System.out.println("加锁成功,执行业务......."+Thread.currentThread().getId());
            Thread.sleep(30000);
        }catch (Exception e){

        }finally {
            System.out.println("解锁成功......."+Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }

    @GetMapping("write")
    @ResponseBody
    public String writeValue(){
        String s = "";
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = lock.writeLock();
        rLock.lock();
        System.out.println("写锁加锁成功......."+Thread.currentThread().getId());
        try {
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            stringRedisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
            System.out.println("写锁解锁成功......."+Thread.currentThread().getId());
        }
        return s;
    }


    @GetMapping("read")
    @ResponseBody
    public String readValue(){
        String s = "";
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = lock.readLock();
        rLock.lock();
        System.out.println("读锁加锁成功......."+Thread.currentThread().getId());
        try {
            s = stringRedisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
            System.out.println("读锁解锁成功......."+Thread.currentThread().getId());
        }
        return s;
    }

    @GetMapping("park")
    @ResponseBody
    public  String park() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
//        park.acquire();
        boolean b = park.tryAcquire();
        return "ok=>"+b;
    }

    @GetMapping("go")
    @ResponseBody
    public  String go() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        park.release();
        return "ok";
    }

    @GetMapping("lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放假了....";
    }


    @GetMapping("gogogo/{id}")
    @ResponseBody
    public String gogogo(@PathVariable("id") int id){
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.countDown();
        return id+"班全走了";
    }
}
